const { DataTypes } = require('sequelize');
const seq = require('../db/seq');

const Order = seq.define('bs_order', {
    order_admin: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单管理账号'
    },
    order_name: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单名称'
    },
    order_id: {
        type: DataTypes.STRING,
        allowNull: false,
        foreignKey: 'order_id',
        unique: true,
        comment: '订单编号'
    },
    order_province: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单省份'
    },
    order_price: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
        comment: '订单总价格'
    },
    order_status: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '订单状态'
    },
    order_remark: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: '订单备注'
    },
    order_address: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单收货地址'
    },
    order_phone: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单联系电话'
    },
});

const OrderGoods = seq.define('bs_order_goods', {
    order_id: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单编号'
    },
    goods_id: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '商品编号'
    },
    goods_name: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '商品名称'
    },
    goods_num: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '商品数量'
    }
});

const Commodity = seq.define('bs_commodity', {
    commodity_id: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '商品编号'
    },
    commodity_name: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '商品名称'
    },
    commodity_price: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
        comment: '商品价格'
    },
    commodity_desc: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '商品描述'
    },
    commodity_status: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '商品状态'
    },
    commodity_inventory: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '商品库存'
    }
});
const ADDpay = seq.define('bs_addpay', {
    pay_id: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '支付账号'
    },
    order_id: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单编号'
    },
    pay_url: {
        type: DataTypes.TEXT,
        allowNull: false,
        comment: '支付链接'
    },
    pay_number: {
        type: DataTypes.STRING,
        allowNull: false,        
        comment: '支付流水号'
    }

});
 
const Logistics = seq.define('bs_logistics', {
    order_id: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '订单编号'
    },
    logistics_name: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '物流名称'
    },
    logistics_number: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '物流单号'
    },
    logistics_time: {    
        type: DataTypes.DATE,   
        allowNull: false,
        comment: '物流发货时间'
    }   
}
);


//Logistics.sync({ force: true })

//ADDpay.sync({ force: true })


Order.hasMany(OrderGoods, { foreignKey: 'order_id', sourceKey: 'order_id', as: 'bs_order_goods' });
OrderGoods.belongsTo(Order, { foreignKey: 'order_id', targetKey: 'order_id', as: 'bs_order' });

module.exports = {
    Order,
    OrderGoods,
    Commodity,
    ADDpay,
    Logistics

};